/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sistema.aplicacao;

import java.awt.Color;
import java.awt.Font;
import java.awt.Paint;
import java.util.List;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.CategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

/**
 *
 * @author gr
 */
public class Graficos
{
  /**
  * Classe interna para geração de labels
  *
  * @param Classe interna para geração de labels
  */

    static class LabelGenerator implements CategoryItemLabelGenerator
    {

        private double threshold;

        public LabelGenerator( double threshold )
        {
            this.threshold = threshold;
        }

        public String generateLabel( CategoryDataset dataset,
                int series, int category )
        {
            String result = null;
            final Number value = dataset.getValue( series, category );
            if ( value != null )
            {
                final double v = value.doubleValue();
                if ( v > this.threshold )
                {
                    result = value.toString(); // could apply formatting here
                }
            }
            return result;
        }

        public String generateRowLabel( CategoryDataset cd, int i )
        {
            throw new UnsupportedOperationException( "Not supported yet." );
        }

        public String generateColumnLabel( CategoryDataset cd, int i )
        {
            throw new UnsupportedOperationException( "Not supported yet." );
        }
    }

    /**
     * Returns Grafico Coluna
     *
     * @return Grafico Coluna
     */
    public static JFreeChart graficoColuna( String titulo, List<Integer> resultados, List<String> periodos )
    {

        //Criação do dataset

        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        for ( int x = 0; x < periodos.size(); x++ )
        {
            dataset.addValue( resultados.get(x), "Ligações", periodos.get(x) );
        }


        //Cria o grafico

        // create the chart...
        JFreeChart chart = ChartFactory.createBarChart(
                titulo, // chart title
                "Período", // domain axis label
                "Ligações", // range axis label
                dataset, // data
                PlotOrientation.VERTICAL, // orientation
                false, // include legend
                true, // tooltips?
                false // URLs?
                );
        chart.setBackgroundPaint( Color.white );
        CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint( Color.lightGray );
        plot.setDomainGridlinePaint( Color.white );
        plot.setRangeGridlinePaint( Color.white );
        NumberAxis rangeAxis = ( NumberAxis ) plot.getRangeAxis();
        rangeAxis.setUpperMargin( 0.15 );
        CategoryItemRenderer renderer = plot.getRenderer();
        //renderer.setLabelGenerator(new LabelGenerator(50.0));
        renderer.setItemLabelGenerator( new LabelGenerator( 0 ) );
        renderer.setItemLabelFont( new Font( "Serif", Font.PLAIN, 10 ) );
        renderer.setItemLabelsVisible( true );




        return chart;

    }

    /**
     * Returns Grafico Pizza
     *
     * @return Grafico Pizza
     */
    public static JFreeChart graficoPizza( String titulo, List<Integer> resultados, List<String> entidades )
    {
        DefaultPieDataset dataset = new DefaultPieDataset();


        for ( int x = 0; x < entidades.size(); x++ )
        {
            dataset.setValue( entidades.get(x), resultados.get(x) );
        }



        // create a chart...
        JFreeChart chart = ChartFactory.createPieChart(
                titulo,
                dataset,
                false,
                // legend?
                true,
                // tooltips?
                false // URLs?
                );

        return chart;

    }
}
